package com.quanyue.music.system.service.impl;

import com.quanyue.auto.util.AssertUtils;
import com.quanyue.music.domain.constant.FMChannel;
import com.quanyue.music.domain.constant.FMPlayRecordType;
import com.quanyue.music.domain.constant.TimeZoneType;
import com.quanyue.music.domain.entity.CenterStatus;
import com.quanyue.music.system.mapper.CenterStatusMapper;
import com.quanyue.music.system.mapper.PlayListMapper;
import com.quanyue.music.system.service.CenterService;
import com.quanyue.music.system.type.CenterShowList;
import com.quanyue.music.system.type.CenterShowResult;
import com.quanyue.music.system.type.FMPlayRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CenterServiceImpl implements CenterService {
    private List<CenterShowList> showLists = new ArrayList<>();
    private List<FMPlayRecord> playRecords = new ArrayList<>();

    @Autowired
    private PlayListMapper playListMapper;
    @Autowired
    private CenterStatusMapper centerStatusMapper;

    @Override
    public void updateStatus(CenterStatus centerStatus) {
        CenterStatus check = centerStatusMapper.findByScopeAndType(centerStatus.getScope(), centerStatus.getType());
        AssertUtils.notNull(check, "请检查更新条件");
        check.setStatus(centerStatus.getStatus());
        centerStatusMapper.update(check);
    }

    @Override
    public CenterShowResult findShowList() {
        showLists.forEach(lists -> {
            List<FMPlayRecord> fmPlayRecords = new ArrayList<>();
            LocalDateTime zoneTime = LocalDateTime.now();
            switch (lists.getZoneType()) {
                case PST:
                    zoneTime = LocalDateTime.now().minusHours(3);
                    break;
                case CST:
                    zoneTime = LocalDateTime.now().minusHours(1);
                    break;
                case MST:
                    zoneTime = LocalDateTime.now().minusHours(2);
                    break;
                default:
                    break;
            }
            for (FMPlayRecord record : playRecords) {
                FMPlayRecord fmPlayRecord;
                switch (record.getPlayRecordType()) {
                    case PLAY_NOW:
                        fmPlayRecord = playListMapper.findCenterPlay(zoneTime, lists.getFmChannel());
                        if (fmPlayRecord == null) fmPlayRecord = new FMPlayRecord();
                        fmPlayRecord.setPlayRecordType(record.getPlayRecordType());
                        fmPlayRecords.add(fmPlayRecord);
                        break;
                    case UP_ONE:
                        fmPlayRecord = playListMapper.findCenterPlay(zoneTime.minusMinutes(30), lists.getFmChannel());
                        if (fmPlayRecord == null) fmPlayRecord = new FMPlayRecord();
                        fmPlayRecord.setPlayRecordType(record.getPlayRecordType());
                        fmPlayRecords.add(fmPlayRecord);
                        break;
                    case UP_TWO:
                        fmPlayRecord = playListMapper.findCenterPlay(zoneTime.minusMinutes(60), lists.getFmChannel());
                        if (fmPlayRecord == null) fmPlayRecord = new FMPlayRecord();
                        fmPlayRecord.setPlayRecordType(record.getPlayRecordType());
                        fmPlayRecords.add(fmPlayRecord);
                        break;
                    case NEXT_ONE:
                        fmPlayRecord = playListMapper.findCenterPlay(zoneTime.plusMinutes(30), lists.getFmChannel());
                        if (fmPlayRecord == null) fmPlayRecord = new FMPlayRecord();
                        fmPlayRecord.setPlayRecordType(record.getPlayRecordType());
                        fmPlayRecords.add(fmPlayRecord);
                        break;
                    case NEXT_TWO:
                        fmPlayRecord = playListMapper.findCenterPlay(zoneTime.plusMinutes(60), lists.getFmChannel());
                        if (fmPlayRecord == null) fmPlayRecord = new FMPlayRecord();
                        fmPlayRecord.setPlayRecordType(record.getPlayRecordType());
                        fmPlayRecords.add(fmPlayRecord);
                        break;
                    default:
                        break;
                }
            }
            lists.setPlayRecords(fmPlayRecords);
        });
        CenterShowResult result = new CenterShowResult();
        result.setCenterShowLists(showLists);
        return result;
    }

    @PostConstruct
    public void init() {
        for (int j = 0; j < 8; j++) {
            CenterShowList showList = new CenterShowList();
            FMPlayRecord fmPlayRecord = new FMPlayRecord();
            switch (j) {
                case 0:
                    fmPlayRecord.setPlayRecordType(FMPlayRecordType.PLAY_NOW);
                    playRecords.add(fmPlayRecord);
                    showList.setFmChannel(FMChannel.CHANNEL_ONE);
                    showList.setZoneType(TimeZoneType.EST);
                    showLists.add(showList);
                    break;
                case 1:
                    fmPlayRecord.setPlayRecordType(FMPlayRecordType.NEXT_ONE);
                    playRecords.add(fmPlayRecord);
                    showList.setFmChannel(FMChannel.CHANNEL_ONE);
                    showList.setZoneType(TimeZoneType.CST);
                    showLists.add(showList);
                    break;
                case 2:
                    fmPlayRecord.setPlayRecordType(FMPlayRecordType.NEXT_TWO);
                    playRecords.add(fmPlayRecord);
                    showList.setFmChannel(FMChannel.CHANNEL_ONE);
                    showList.setZoneType(TimeZoneType.MST);
                    showLists.add(showList);
                    break;
                case 3:
                    fmPlayRecord.setPlayRecordType(FMPlayRecordType.UP_ONE);
                    playRecords.add(fmPlayRecord);
                    showList.setFmChannel(FMChannel.CHANNEL_ONE);
                    showList.setZoneType(TimeZoneType.PST);
                    showLists.add(showList);
                    break;
                case 4:
                    fmPlayRecord.setPlayRecordType(FMPlayRecordType.UP_TWO);
                    playRecords.add(fmPlayRecord);
                    showList.setFmChannel(FMChannel.CHANNEL_TWO);
                    showList.setZoneType(TimeZoneType.EST);
                    showLists.add(showList);
                    break;
                case 5:
                    showList.setFmChannel(FMChannel.CHANNEL_TWO);
                    showList.setZoneType(TimeZoneType.CST);
                    showLists.add(showList);
                    break;
                case 6:
                    showList.setFmChannel(FMChannel.CHANNEL_TWO);
                    showList.setZoneType(TimeZoneType.MST);
                    showLists.add(showList);
                    break;
                case 7:
                    showList.setFmChannel(FMChannel.CHANNEL_TWO);
                    showList.setZoneType(TimeZoneType.PST);
                    showLists.add(showList);
                    break;
                default:
                    break;
            }
        }
    }
}
